package com.pin.zheng.xingmulibrary.http.net;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.pin.zheng.xingmulibrary.util.LogUtils;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Converter;

/**
 * 创建进行解密操作的Converter 类
 * 如果在发送请求之前做转换，则实现 ResponseBody Converter方法
 *
 * @param <T>
 */
public class DecodeResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private TypeAdapter<T> adapter;
    private Gson gson;

    public DecodeResponseBodyConverter(TypeAdapter<T> adapter) {
        this.adapter = adapter;
    }

    public DecodeResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody responseBody) throws IOException {

        String strRes = responseBody.string();
        LogUtils.d(strRes);
//        String str = EncryptionUtils.decryptDES(strRes);
//        LogUtils.d(str);
        return adapter.fromJson(strRes);
    }
}